﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using weikebuluo.DBUtility;
using System.Data.SqlClient;
using System.Data;

namespace weikebuluo.DAL
{
	/// <summary>
	/// 数据访问类:t_set_dal
	/// </summary>
	public partial class t_set_dal
	{
		DataCreator<weikebuluo.Model.t_set_model> CreateSQL = new DataCreator<weikebuluo.Model.t_set_model>();

		private string tableName = "t_set";
		private string id = "MId";

		public t_set_dal()
		{

		}

		private string AddIdSqlWhere(string id, string value)
		{
			return string.Format(" {0}={1}", id, value);
		}

		#region 增加一条数据
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(weikebuluo.Model.t_set_model model)
		{
			SqlParameter[] parameters = CreateSQL.CreateParameter(model);

			string strSql = CreateSQL.CreateInsertSql(model, tableName, id) + ";select @@IDENTITY";

			object obj = DbHelperSQL.GetSingle(strSql, parameters);

			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		#endregion 增加一条数据

		#region 更新一条数据
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(weikebuluo.Model.t_set_model model)
		{
			SqlParameter[] parameters = CreateSQL.CreateParameter(model);

			string strSql = CreateSQL.CreateUpdateSql(model, tableName, id, AddIdSqlWhere(id, model.MId.ToString()));

			int rows = DbHelperSQL.ExecuteSql(strSql, parameters);

			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		#endregion 更新一条数据

		#region 删除一条数据
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			string strSql = CreateSQL.CreateDeleteSql(tableName, AddIdSqlWhere(id,Id.ToString()));

			int rows = DbHelperSQL.ExecuteSql(strSql);

			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		#endregion 删除一条数据

		#region 得到一个对象实体
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public weikebuluo.Model.t_set_model GetModel(int Id)
		{
			weikebuluo.Model.t_set_model model = new weikebuluo.Model.t_set_model();

			StringBuilder strSql = new StringBuilder();
		
			strSql.Append(string.Format("select top 1 * from {0} where {1}=@{2}", tableName, id, id));

			SqlParameter[] parameters = {
					new SqlParameter("@"+id, SqlDbType.Int,4)
			};

			parameters[0].Value = Id;

			DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

			if (ds.Tables[0].Rows.Count > 0)
			{
				return CreateSQL.GetModel(model, ds);
			}
			else
			{
				return null;
			}
		}
		#endregion 得到一个对象实体

		#region 获得数据列表
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM  t_set");

			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}

			return DbHelperSQL.Query(strSql.ToString());
		}
		#endregion 获得数据列表

		#region 分页获取数据列表
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");

			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby);
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}

			strSql.Append(string.Format(")AS Row, T.*  from {0} T ",tableName));

			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}

			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);

			return DbHelperSQL.Query(strSql.ToString());
		}
		#endregion 分页获取数据列表
	}

}
